import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:spendbook/common/icons.dart';
import 'package:spendbook/common/widgets.dart';
import 'package:spendbook/models/user.dart';

class MyScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final _user = context.read<User>();

    final List<Widget> items = <Widget>[
      MyInfo(user: _user),
      Container(
        height: 10.0,
        decoration: BoxDecoration(color: Theme.of(context).dividerColor),
      ),
      ListTile(
        title: Row(
          children: <Widget>[
            Expanded(flex: 1, child: Text('我的名片')),
            Icon(
              IconFont.qrcode,
              size: 18,
            )
          ],
        ),
        trailing: Icon(Icons.chevron_right),
        onTap: () {
          Navigator.pushNamed(context, '/qrcode');
        },
      ),
      ListTile(
        title: Text('我的统计'),
        trailing: Icon(Icons.chevron_right),
        onTap: () {
          Navigator.pushNamed(context, '/chart');
        },
      ),
      ListTile(
        title: Text('家庭成员'),
        trailing: Icon(Icons.chevron_right),
        onTap: () {
          Navigator.pushNamed(context, '/family');
        },
      ),
      ListTile(
        title: Text('常用分类'),
        trailing: Icon(Icons.chevron_right),
        onTap: () {
          Navigator.pushNamed(context, '/category');
        },
      ),
      ListTile(
        title: Text('设置'),
        trailing: Icon(Icons.chevron_right),
        onTap: () {
          Navigator.pushNamed(context, '/setting');
        },
      ),
      Container(
        height: 10.0,
        decoration: BoxDecoration(color: Theme.of(context).dividerColor),
      ),
      ListTile(
        title: Center(
          child: Text('注销帐号'),
        ),
        onTap: () async {
          final res = await showOkCancelAlertDialog(context: context, title: '退出提示', message: '是否注销当前帐号?', okLabel: '是', cancelLabel: '否');
          if (res == OkCancelResult.ok) {
            _user.logout();
          }
        },
      ),
    ];

    return Scaffold(
      appBar: PreferredSize(
        child: AppBar(
          backgroundColor: Colors.transparent,
          elevation: 0,
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.camera_alt),
              color: _user.isDarkTheme ? Colors.white : Theme.of(context).colorScheme.secondary,
              onPressed: () {
                Navigator.pushNamed(context, '/scan');
              },
            )
          ],
        ),
        preferredSize: Size.fromHeight(35.0),
      ),
      body: ListView.separated(
        separatorBuilder: listSeparatorBuilder,
        itemCount: items.length,
        itemBuilder: (BuildContext context, int index) {
          return items[index];
        },
      ),
    );
  }
}

class MyInfo extends StatefulWidget {
  MyInfo({Key? key, required this.user}) : super(key: key);

  final User user;

  @override
  _MyInfoState createState() => _MyInfoState();
}

class _MyInfoState extends State<MyInfo> {
  TextEditingController _nameCtrl = TextEditingController();
  late User _user;

  @override
  void initState() {
    super.initState();
    _user = widget.user;
  }

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: CircleAvatar(
        child: Container(
          height: 60.0,
          width: 60.0,
          child: Center(
            child: Text(_user.nickname.length > 0 ? _user.nickname.substring(0, 1) : '未'),
          ),
        ),
      ),
      title: GestureDetector(
        child: _user.nickname.length > 0
            ? Text(_user.nickname)
            : Text(
                '未设置',
                style: TextStyle(decoration: TextDecoration.underline),
              ),
        onTap: () async {
          String? _name = await showDialog(
            context: context,
            builder: (context) {
              _nameCtrl.text = _user.nickname;
              return AlertDialog(
                  title: Text('清输入你的昵称'),
                  content: TextFormField(
                    controller: _nameCtrl,
                    autofocus: true,
                  ),
                  actions: <Widget>[
                    TextButton(
                      child: Text('提交'),
                      onPressed: () {
                        Navigator.of(context).pop(_nameCtrl.value.text);
                        _nameCtrl.clear();
                      },
                    ),
                  ]);
            },
          );
          if (_name != null) {
            setState(() {
              _user.nickname = _name;
            });
            await _user.store();
          }
        },
      ),
      subtitle: Text(_user.phone),
    );
  }
}
